<?php


if (isset($_GET['action']) && $_GET['action'] == "edit" && isset($_GET['team_id'])) {

	$team_id = $_GET['team_id'];

	$team_item = $team->get($team_id);

	$team_item = $team_item[0];

	$smarty->assign('team_item', $team_item);

	$smarty->assign('fileaction', "edit");

	$smarty->display('team_edit.tpl', $_SESSION['uid']);

}elseif (isset($_GET['action']) && $_GET['action'] == "new") {

	$smarty->assign('fileaction', "new");

	$smarty->display('team_edit.tpl', $_SESSION['uid']);

}elseif (isset($_GET['action']) && $_GET['action'] == "remove" && is_allowed(3))  {

	$team_id = $_GET['team_id'];

	$team->remove($team_id);

	$teams = $team->get();

	$smarty->assign('team', $teams);

	$smarty->assign('message', "The team has been removed!");

	$smarty->display('team_list.tpl', $_SESSION['uid']);

}elseif (isset($_POST['action']) && $_POST['action'] == "save") {


	$team_id 		= $_POST['team_id'];
	$team_name 		= $_POST['team_name'];
	$team_image		= $_POST['team_image'];
	$team_order 	= $_POST['team_order'];

	if($_POST['fileaction'] == "edit") {
		if($_FILES["team_image"]['error'] == 4) {

			$team_item = $team->get($team_id);

			$team_item = $team_item[0];

			$team_image = $team_item['team_image'];

		}
	} else {
		$team_image = "";
	}



	if(!empty($_FILES["team_image"])) {

		$downloaddir = $_SERVER['DOCUMENT_ROOT'] . "/images/ranks/";

		if(move_uploaded_file($_FILES["team_image"]["tmp_name"], $downloaddir . $_FILES["team_image"]["name"])) {

			$team_image	= "./images/ranks/" . $_FILES["team_image"]["name"];

		} else {

		}
	}

	$team->save($team_id, $team_name, $team_image, $team_order);

	$smarty->assign('message', "The team has been changed/added!");

	$smarty->display('team_edit.tpl', $_SESSION['uid']);

} else {

	$teams = $team->get();

	$smarty->assign('team', $teams);

	$smarty->display('team_list.tpl', $_SESSION['uid']);
}





?>
